import { request } from '../../utils/http'
var http = require("../../utils/http.js");
// pages/cart/cart.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // carList: [],
        // id: "",
        // picDomain: config.picDomain,
        shopCartItemDiscounts: [],
        finalMoney: 0,
        totalMoney: 0,
        subtractMoney: 0,
        allChecked: false
    },
    cart() {
        request({
            url: "/p/shopCart/info",
            method: "POST",
            header: {
                'Authorization': "ftYOutnCxkHPaKhCFLDGxXC22YEvRrRjGGQCSb2+zUCsFjPFFrx5rJoLEyZ9SP6V"
            },

            data: {},
        }).then(res => {
            if (res.length > 0) {
                // 默认不选中
                var shopCartItemDiscounts = res[0].shopCartItemDiscounts;

                shopCartItemDiscounts.forEach(shopCartItemDiscount => {
                    shopCartItemDiscount.shopCartItems.forEach(shopCartItem => {
                        shopCartItem.checked = false;
                    });
                });
                this.setData({
                    shopCartItemDiscounts: shopCartItemDiscounts,
                    allChecked: false
                });
            } else {
                this.setData({
                    shopCartItemDiscounts: []
                });
            }
            this.calTotalPrice()
        })
    },
    /**
   * 减少数量
   */
    onCountMinus(e) {
        var index = e.currentTarget.dataset.index;
        var scindex = e.currentTarget.dataset.scindex;
        var shopCartItemDiscounts = this.data.shopCartItemDiscounts;
        var prodCount = shopCartItemDiscounts[scindex].shopCartItems[index].prodCount;
        if (prodCount > 1) {
            this.updateCount(shopCartItemDiscounts, scindex, index, -1);
        }
    },

    /**
     * 增加数量
     */
    onCountPlus(e) {

        var index = e.currentTarget.dataset.index;
        var scindex = e.currentTarget.dataset.scindex;
        var shopCartItemDiscounts = this.data.shopCartItemDiscounts;
        this.updateCount(shopCartItemDiscounts, scindex, index, 1);
    },

    /**
     * 改变购物车数量接口
     */
    updateCount(shopCartItemDiscounts, scindex, index, prodCount) {
        var ths = this;
        request({
            url: "/p/shopCart/changeItem",
            method: "POST",
            data: {
                count: prodCount,
                prodId: shopCartItemDiscounts[scindex].shopCartItems[index].prodId,
                skuId: shopCartItemDiscounts[scindex].shopCartItems[index].skuId,
                shopId: 1
            },

        }).then(res => {
            shopCartItemDiscounts[scindex].shopCartItems[index].prodCount += prodCount;
            ths.setData({
                shopCartItemDiscounts: shopCartItemDiscounts
            });
            ths.calTotalPrice();//计算总价
            http.getCartCount();//重新计算购物车总数量
        })

    },

    loadBasketData() {
        request({
            url: "/p/shopCart/info",
            method: "POST",
            data: {},
            header: {
                'Authorization': "ftYOutnCxkHPaKhCFLDGxXC22YEvRrRjGGQCSb2+zUCsFjPFFrx5rJoLEyZ9SP6V"
            },
        }).then(res => {

            if (res.length > 0) {
                // 默认不选中
                var shopCartItemDiscounts = res[0].shopCartItemDiscounts;
                shopCartItemDiscounts.forEach(shopCartItemDiscount => {
                    shopCartItemDiscount.shopCartItems.forEach(shopCartItem => {
                        shopCartItem.checked = false;
                    });
                });
                this.setData({
                    shopCartItemDiscounts: shopCartItemDiscounts,
                    allChecked: false
                });
            } else {
                this.setData({
                    shopCartItemDiscounts: []
                });
            }

            this.calTotalPrice(); //计算总价

        })


    },
    /**
     * 删除购物车商品
     */
    onDelBasket() {
        var ths = this;

        var shopCartItemDiscounts = this.data.shopCartItemDiscounts;
        var basketIds = [];
        for (var i = 0; i < shopCartItemDiscounts.length; i++) {
            var cItems = shopCartItemDiscounts[i].shopCartItems;
            for (var j = 0; j < cItems.length; j++) {
                if (cItems[j].checked) {
                    basketIds.push(cItems[j].basketId);
                }
            }
        }

        if (basketIds.length == 0) {
            wx.showToast({
                title: '请选择商品',
                icon: "none",


            })
        } else {
            wx.showModal({
                title: '',
                content: '确认要删除选中的商品吗？',
                confirmColor: "#eb2444",
                success(res) {
                    if (res.confirm) {
                        request({
                            url: "/p/shopCart/deleteItem",
                            method: "DELETE",
                            data: basketIds,
                            header: {
                                'Authorization': "ftYOutnCxkHPaKhCFLDGxXC22YEvRrRjGGQCSb2+zUCsFjPFFrx5rJoLEyZ9SP6V"
                            },

                        }).then(res => {
                            ths.loadBasketData();
                        })
                    }
                }
            })
        }
    },

    /**
 * 全选
 */
    onSelAll() {
        var allChecked = this.data.allChecked;
        allChecked = !allChecked; //改变状态
        var shopCartItemDiscounts = this.data.shopCartItemDiscounts;

        for (var i = 0; i < shopCartItemDiscounts.length; i++) {
            var cItems = shopCartItemDiscounts[i].shopCartItems;
            for (var j = 0; j < cItems.length; j++) {
                cItems[j].checked = allChecked;
            }
        }

        this.setData({
            allChecked: allChecked,
            shopCartItemDiscounts: shopCartItemDiscounts
        });
        this.calTotalPrice();//计算总价
    },
    /**
   * 检查全选状态
   */
    checkAllSelected() {
        var allChecked = true;
        var shopCartItemDiscounts = this.data.shopCartItemDiscounts;

        var flag = false;
        for (var i = 0; i < shopCartItemDiscounts.length; i++) {
            var cItems = shopCartItemDiscounts[i].shopCartItems;
            for (var j = 0; j < cItems.length; j++) {
                if (!cItems[j].checked) {
                    allChecked = !allChecked;
                    flag = true;
                    break;
                }
            }
            if (flag) {
                break;
            }
        }
        this.setData({
            allChecked: allChecked
        });
    },
    onSelectedItem(e) {
        var index = e.currentTarget.dataset.index;// 获取data- 传进来的index
        var scindex = e.currentTarget.dataset.scindex;

        var shopCartItemDiscounts = this.data.shopCartItemDiscounts;// 获取购物车列表
        var checked = shopCartItemDiscounts[scindex].shopCartItems[index].checked; // 获取当前商品的选中状态
        shopCartItemDiscounts[scindex].shopCartItems[index].checked = !checked; // 改变状态
        this.setData({
            shopCartItemDiscounts: shopCartItemDiscounts
        });
        this.checkAllSelected();//检查全选状态
        this.calTotalPrice();//计算总价
    },
    /**
 * 计算购物车总额
 */
    calTotalPrice() {
        var shopCartItemDiscounts = this.data.shopCartItemDiscounts;

        var shopCartIds = [];
        for (var i = 0; i < shopCartItemDiscounts.length; i++) {
            var cItems = shopCartItemDiscounts[i].shopCartItems;
            for (var j = 0; j < cItems.length; j++) {
                if (cItems[j].checked) {
                    shopCartIds.push(cItems[j].basketId);
                }
            }
        }
        var ths = this;
        request({
            url: "/p/shopCart/totalPay",
            method: "POST",
            data: shopCartIds,
            header: {
                'Authorization': "ftYOutnCxkHPaKhCFLDGxXC22YEvRrRjGGQCSb2+zUCsFjPFFrx5rJoLEyZ9SP6V"
            },
        }).then(res => {

            ths.setData({
                finalMoney: res.finalMoney,
                totalMoney: res.totalMoney,
                subtractMoney: res.subtractMoney
            });
        })
    },

    /**
  * 去结算
  */
    toFirmOrder() {
        var shopCartItemDiscounts = this.data.shopCartItemDiscounts;
        var basketIds = [];
        shopCartItemDiscounts.forEach(shopCartItemDiscount => {
            shopCartItemDiscount.shopCartItems.forEach(shopCartItem => {
                if (shopCartItem.checked) {
                    basketIds.push(shopCartItem.basketId)
                }
            })
        })
        if (!basketIds.length) {
            wx.showToast({
                title: '请选择商品',
                icon: "none"
            })
            return
        }
        wx.setStorageSync("basketIds", JSON.stringify(basketIds));
        wx.navigateTo({
            url: '/pages/baybuy/baybuy?orderEntry=0',
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.cart()

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.loadBasketData();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },



})